import { useUserStore } from '@/store';

export default function usePermission() {
  const userStore = useUserStore();
  return {
    accessRouter(route) {
      console.log(
        7,
        userStore.userInfo?.roles?.find((el) => el == route.meta?.roles)
      );
      return !route.meta?.requiresAuth || !route.meta?.roles || userStore.userInfo?.roles?.find((el) => el == route.meta?.roles);
    },
    findFirstPermissionRoute(_routers, role) {
      const cloneRouters = [..._routers];
      console.log(15, cloneRouters, _routers);
      while (cloneRouters.length) {
        const firstElement = cloneRouters.shift();
        console.log(17, firstElement);
        if (role?.find((el) => el == firstElement?.meta?.roles)) return { name: firstElement.name };
        if (firstElement?.children) {
          cloneRouters.push(...firstElement.children);
        }
      }
      return null;
    }
    // You can add any rules you want
  };
}
